import util.file as RF
import util.IO as RI
import util.math as RM
import statisticTest.Statistic as RS
class Analyse:
    def __init__(self,dataBasePath,gap='\\'):
        self.__data=RF.read(dataBasePath,gap=gap)
        self.__data.pop(0)
        self.__sampleSize=self.__data.__len__()
        self.__staData=[]
        self.__reduce =[]
        self.__nor =[]
        self.__caseCategory=['字符串','数组','线性表','数字操作','图结构','树结构','查找算法','排序算法']
        self.__caseCategoryData=[]
        for i in range(8):
            self.__caseCategoryData.append([0,0,0,0,0,0])
        for i in range(self.__sampleSize):
            for j in range(self.__caseCategory.__len__()):
                if self.__caseCategory[j]==self.__data[i][1]:
                    self.__caseCategoryData[j][0]+=1
                    self.__caseCategoryData[j][1] += float(self.__data[i][3])
                    self.__caseCategoryData[j][2] += float(self.__data[i][4])
                    self.__caseCategoryData[j][3] += float(self.__data[i][5])
                    self.__caseCategoryData[j][4] += float(self.__data[i][6])
                    self.__caseCategoryData[j][5] += float(self.__data[i][7])
            addLine=[]
            addLine.append(int(self.__data[i][0]))
            addLine.append(float(self.__data[i][3]))
            addLine.append(float(self.__data[i][4]))
            addLine.append(float(self.__data[i][5]))
            addLine.append(float(self.__data[i][6]))
            addLine.append(float(self.__data[i][7]))
            self.__staData.append(addLine)
        #算平均数
        for i in range(8):
            for j in range(1,6):
                self.__caseCategoryData[i][j]/=self.__caseCategoryData[i][0]

    def draw(self):
        x=[]
        for i in range(self.__sampleSize):
            addLine=self.__staData[i].copy()
            addLine.pop(0)
            x.append(addLine)
        self.__reduce=RM.reduceDim(x,0.99)
        self.__nor=RM.normalize(self.__reduce,0,1)
        for i in range(self.__nor.__len__()):
            self.__nor[i].insert(0,self.__staData[i][0])
        for i in range(0,self.__sampleSize-1):
            for j in range(i+1,self.__sampleSize):
                if self.__nor[i][1]<self.__nor[j][1]:
                    temp=self.__nor[i]
                    self.__nor[i]=self.__nor[j]
                    self.__nor[j]=temp
    def barGraph(self):
        values=[]
        for i in range(8):
            values.append([])
        for i in range(1,6):
            for j in range(8):
                values[j].append(self.__caseCategoryData[j][i])
        values=RM.reduceDim(values,0.99)
        values=RM.normalize(values,0,1)
        values=[line[0] for line in values]
        category=['string','array','list','num','graph','tree','search','arrange']
        for i in range(7):
            for j in range(i+1,8):
                if values[i]<values[j]:
                    temp=values[i]
                    values[i]=values[j]
                    values[j]=temp
                    temp=category[i]
                    category[i]=category[j]
                    category[j]=temp
        RI.barGraph(values,'case category','average people','',category)

    def score(self):
        statistic = RS.Statistic(RF.getRootPath() + 'data\dataBase\storage.csv', '\\')
        array=[[4,8,2,3,1,6,7,5],[4,3,2,7,8,6,1,5],[3,4,7,2,8,6,5,1],[3,4,8,2,1,7,6,5],[8,7,2,1,4,3,6,5]]
        scores=[0,0,0,0,0]
        for i in range(5):
            for j in range(5):
                if i!=j:
                    scores[i]+=statistic.sortScore(array[i],array[j])
        scores=[i/4 for i in scores]
        print(scores)

